<template>
  <div class="maincontain">
    <div class="filter-bar"></div>
    <div class="userlist">
      <a-table
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
        :dataSource="dataSource"
        :columns="columns"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space wrap>
              <a-button
                size="small"
                type="primary"
                @click="handleAgree(record)"
                :disabled="record.state !==0"
                >同意</a-button
              >
              <a-button
                danger
                size="small"
                @click="handleReject(record)"
                :disabled="record.state !== 0"
                >拒绝</a-button
              >
            </a-space>
          </template>
        </template>
      </a-table>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, createVNode, onMounted, ref } from "vue";

import {
  studentlabArrangeQuery,
  updataStudentApply,
} from "@/api/methods/labarrangeReq";

import { omit } from "lodash";
import { useRouter } from "vue-router";
import { FormType } from "@/EnvEnum/form";
import { Message } from "@/utils/message";

const router = useRouter();
const dataSource = ref();
const pageConfig = ref({
  pageSize: 10,
  page: 1,
  total: 0,
});

const columns = [
  { title: "申请实验室", dataIndex: "labName", key: "labName", width: "" },
  { title: "申请学期", dataIndex: "semesterName", key: "semesterName" },
  {
    title: "周次",
    dataIndex: "week",
    key: "week",
    customRender: ({ text, record }) => {
      return translateWeek(text);
    },
  },
  {
    title: "星期",
    dataIndex: "day",
    key: "day",
    customRender: ({ text, record }) => {
      return translateDay(text);
    },
  },
  {
    title: "节次",
    dataIndex: "session",
    key: "session",
    customRender: ({ text, record }) => {
      return translateSession(text);
    },
  },
  { title: "申请理由", dataIndex: "reason", key: "reason" },
  {
    title: "使用情况",
    dataIndex: "state",
    key: "state",
    customRender: ({ text, record }) => {
      return translateState(text);
    },
  },
  { title: "操作", dataIndex: "action", key: "action", width: "160px" },
];

interface IPagenation {
  current: number;
  pageSize: number;
  total: number;
}
const pagination = computed(() => ({
  total: pageConfig.value.total,
  current: pageConfig.value.page,
  pageSize: pageConfig.value.pageSize,
}));
const loading = ref(false);
const handleTableChange = async (pagination: IPagenation) => {
  pageConfig.value.page = pagination.current;
  pageConfig.value.pageSize = pagination.pageSize;
  loading.value = true;
  const { data: res } = await studentlabArrangeQuery(
    omit(pageConfig.value, "total")
  );
  reflectMapDataSource(res);
  loading.value = false;
};
onMounted(async () => {
  loading.value = true;
  const { data: res } = await studentlabArrangeQuery(
    omit(pageConfig.value, "total")
  );
  reflectMapDataSource(res);
  loading.value = false;
});
const reflectMapDataSource = (res: any) => {
  pageConfig.value.total = res.total;

  dataSource.value = res.records.map((item: any) => {
    return {
      id: item.id,
      labId: item.labId,
      labName: item.labName,
      semesterId: item.semesterId,
      semesterName: item.semesterName,
      week: item.week,
      day: item.day,
      session: item.session,
      state: item.state,
      reason: item.reason,
      studentId: item.studentId,
      studentName: item.studentName,
    };
  });
};

async function handleAgree(record: any) {
  try {
    const updatedRecord = { ...record, state: 1 };
    await updataStudentApply(updatedRecord);
    reload();
    Message("success", "已同意申请");
  } catch (error) {
    Message("error", "同意申请失败，请稍后重试");
  }
}

async function handleReject(record: any) {
  try {
    const updatedRecord = { ...record, state: 2 };
    await updataStudentApply(updatedRecord);
    reload();
    Message("success", "已拒绝申请");
  } catch (error) {
    Message("error", "拒绝申请失败，请稍后重试");
  }
}

async function reload() {
  loading.value = true;
  const { data: res } = await studentlabArrangeQuery(
    omit(pageConfig.value, "total")
  );
  reflectMapDataSource(res);
  loading.value = false;
}

const translateState = (state: number) => {
  switch (state) {
    case 0:
      return "待审核";
    case 1:
      return "通过";
    case 2:
      return "驳回";
    case 3:
      return "已撤回申请";
    case 4:
      return "使用完毕";
    default:
      return "未知";
  }
};

const translateWeek = (week) => {
  return "第" + week + "周";
};

const translateDay = (day) => {
  switch (day) {
    case 1:
      return "星期一";
    case 2:
      return "星期二";
    case 3:
      return "星期三";
    case 4:
      return "星期四";
    case 5:
      return "星期五";
    case 6:
      return "星期六";
    case 7:
      return "星期日";
    default:
      return "--";
  }
};

const translateSession = (session) => {
  switch (session) {
    case 1:
      return "1-2";
    case 2:
      return "3-5";
    case 3:
      return "6-7";
    case 4:
      return "8-9";
    case 5:
      return "10-12";
    case 6:
      return "13-15";
    default:
      return "--";
  }
};
</script>
<style scoped lang="scss">
/* @import url(); 引入css类 */
.maincontain {
  display: flex;
  flex-direction: column;
  .createuser {
    margin-bottom: 15px;
  }
}
</style>
@/EnvEnum/form
